package com.yiren.dbaa.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.google.gson.Gson;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.net.bean.BaseResponseBean;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.interf.StringCallback;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lwb
 * on 2021/11/18
 * Describe  配置保存工具SharedPreferenceUtil
 */

public class SharedPreferenceUtil {

    private static SharedPreferenceUtil sharedPreferenceUtil;
    private static SharedPreferences sharedPreferences;

    private final static String KEY = "dj_config_sharepreferences_k3";

    private Context context;

    private SharedPreferenceUtil(Context context) {
        sharedPreferences = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
        this.context = context;
    }

    public static SharedPreferenceUtil getInstance(Context context) {
        if (sharedPreferenceUtil == null) {
            sharedPreferenceUtil = new SharedPreferenceUtil(context);
        }
        return sharedPreferenceUtil;
    }

    /**
     * 设置String类型值
     *
     * @param key
     * @param value
     */
    public void putString(String key, String value, boolean isSynToTheServer) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
        if (isSynToTheServer) {
            localSynPreferences(context, key, value, "String");
        }
    }

    /**
     * 设置long类型值
     *
     * @param key
     * @param value
     */
    public void putLong(String key, long value, boolean isSynToTheServer) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(key, value);
        editor.commit();
        if (isSynToTheServer) {
            localSynPreferences(context, key, value, "long");
        }
    }

    /**
     * 设置int类型值
     *
     * @param key
     * @param value
     */
    public void putInt(String key, int value, boolean isSynToTheServer) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
        if (isSynToTheServer) {
            localSynPreferences(context, key, value, "int");
        }
    }

    /**
     * 设置Boolean类型值
     *
     * @param key
     * @param value
     */
    public void putBoolean(String key, boolean value, boolean isSynToTheServer) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
        if (isSynToTheServer) {
            localSynPreferences(context, key, value, "boolean");
        }
    }

    /**
     * 设置Float类型值
     *
     * @param key
     * @param value
     */
    public void putFloat(String key, float value, boolean isSynToTheServer) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putFloat(key, value);
        editor.commit();
        if (isSynToTheServer) {
            localSynPreferences(context, key, value, "float");
        }
    }

    /**
     * 获取key相对应的value，如果不设默认参数，默认值为""
     *
     * @param key
     * @return
     */
    public String getString(String key) {
        return getString(key, "");
    }

    /**
     * 获取key相对应的value，如果不设默认参数，默认值为""
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public String getString(String key, String defaultValue) {
        return sharedPreferences.getString(key, defaultValue);
    }

    /**
     * 获取key相对应的value，如果不设默认参数，默认值为false
     *
     * @param key
     * @return
     */
    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    /**
     * 获取key相对应的value，如果不设默认参数，默认值为false
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        return sharedPreferences.getBoolean(key, defaultValue);
    }

    /**
     * 获取key相对应的value，如果不设默认参数，默认值为0
     *
     * @param key
     * @return
     */
    public int getInt(String key) {
        return getInt(key, 0);
    }

    /**
     * 获取key相对应的value，如果不设默认参数，默认值为0
     *
     * @param key
     * @return
     */
    public int getInt(String key, int defaultValue) {
        return sharedPreferences.getInt(key, defaultValue);
    }

    /**
     * 获取key相对应的value，如果不设默认参数，默认值为0
     *
     * @param key
     * @return
     */
    public long getLong(String key) {
        return getLong(key, 0L);
    }

    /**
     * 获取key相对应的value，如果不设默认参数，默认值为0
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public long getLong(String key, Long defaultValue) {
        return sharedPreferences.getLong(key, defaultValue);
    }

    /**
     * 获取key相对应的value，如果不设默认参数，默认值为0
     *
     * @param key
     * @return
     */
    public float getFloat(String key) {
        return getFloat(key, 0f);
    }

    /**
     * 获取key相对应的value，如果不设默认参数，默认值为0
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public float getFloat(String key, Float defaultValue) {
        return sharedPreferences.getFloat(key, defaultValue);
    }

    /**
     * 判断是否存在此字段
     */
    public boolean contains(String key) {
        return sharedPreferences.contains(key);
    }

    /**
     * 判断是否存在此字段
     */
    public boolean has(String key) {
        return sharedPreferences.contains(key);
    }

    /**
     * 删除sharedPreferences文件中对应的Key和value
     */
    public boolean remove(String key) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(key);
        return editor.commit();
    }


    /**
     * @param context
     * @param key
     * @param value
     * @param type
     */
    public static void localSynPreferences(final Context context, String key, Object value, String type) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("skey", key);
        map.put("svalue", value);
        if ("float".equals(type)) {
            map.put("stype", "float");
        }
        if ("boolean".equals(type)) {
            map.put("stype", "boolean");
        }
        if ("String".equals(type)) {
            map.put("stype", "String");
        }
        if ("long".equals(type)) {
            map.put("stype", "long");
        }
        if ("int".equals(type)) {
            map.put("stype", "int");
        }
        OkHttpUtils.getInstance().postJsonString((Activity) context, AddressConstant.getParamSettingApiUrl(), new Gson().toJson(map), new StringCallback() {
            @Override
            public void getString(String result) {
                try {
                    BaseResponseBean responseBean = new Gson().fromJson(result, BaseResponseBean.class);
                    if (responseBean.getError() == null) {
                        if (null != responseBean.getResult() && (Boolean) responseBean.getResult()) {
                            //								Toast.makeText(context, "设置成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(context, "设置失败", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(context, "设置失败" + responseBean.getError().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


}
